﻿#include "vip_dialog.h"
#include "ui_vip_dialog.h"
#include "basal_sql.h"
#include "addvip_dialog.h"
#include "qdebug.h"
#include <QShowEvent>
#include <QMessageBox>

vip_dialog::vip_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::vip_dialog)
{
    ui->setupUi(this);
    ui->vipTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

vip_dialog::~vip_dialog()
{
    delete ui;
}

//显示超市会员整体数据
void vip_dialog::showEvent(QShowEvent *event)
{
    sqlite3 *pDb;
    if(sqlite3_open("./supermarket.db", &pDb) == SQLITE_OK){
        QString sql="select v_id,v_name,v_cell,v_level from vip;";
        sqlite3_stmt *pStmt;
        if(sqlite3_prepare(pDb, sql.toStdString().c_str(), sql.length(), &pStmt, NULL) == SQLITE_OK){
            while(sqlite3_step(pStmt) == SQLITE_ROW){
                const unsigned char *pId = sqlite3_column_text(pStmt, 0);
                const unsigned char *pName = sqlite3_column_text(pStmt, 1);
                const unsigned char *pCell = sqlite3_column_text(pStmt, 2);
                const unsigned char *pLevel = sqlite3_column_text(pStmt, 3);

                QTableWidgetItem *pIdItem = new QTableWidgetItem((char *)pId);
                QTableWidgetItem *pNameItem = new QTableWidgetItem((char *)pName);
                QTableWidgetItem *pCellItem = new QTableWidgetItem((char *)pCell);
                QTableWidgetItem *pLevelItem = new QTableWidgetItem((char *)pLevel);

                int row = ui->vipTable->rowCount();
                ui->vipTable->insertRow(row);

                ui->vipTable->setItem(row, 0, pIdItem);
                ui->vipTable->setItem(row, 1, pNameItem);
                ui->vipTable->setItem(row, 2, pCellItem);
                ui->vipTable->setItem(row, 3, pLevelItem);

            }
        }
    }
}

//信号槽
void vip_dialog::on_addvipButton_clicked()
{
    addvip_dialog *addNewvip = new addvip_dialog();
    connect(addNewvip, SIGNAL(addVip(QString, QString, QString, QString)),
            this, SLOT(addVip(QString, QString, QString, QString)));
    addNewvip->exec();
}

//槽（接受信号并显示相关信息）
void vip_dialog::addVip(QString id, QString name, QString cell, QString level){

    QSqlQuery query;
    QString sql = "insert into vip(v_id,v_name,v_cell,v_level,v_allconsum) "
                  "values('" +id+ "','" +name+ "','" +cell+ "','" +level+ "','0');";
    if(query.exec(sql)){
        QMessageBox::about(NULL, "提示", "添加成功");

    }else{
        QMessageBox::about(NULL, "提示", "添加失败");
    }

    QTableWidgetItem *pIdItem = new QTableWidgetItem (id);
    QTableWidgetItem *pNameItem = new QTableWidgetItem (name);
    QTableWidgetItem *pCellItem = new QTableWidgetItem (cell);
    QTableWidgetItem *pLevelItem = new QTableWidgetItem (level);

    int row = ui->vipTable->rowCount();
    ui->vipTable->insertRow(row);

    ui->vipTable->setItem(row, 0, pIdItem);
    ui->vipTable->setItem(row, 1, pNameItem);
    ui->vipTable->setItem(row, 2, pCellItem);
    ui->vipTable->setItem(row, 3, pLevelItem);
}

//删除会员（操作数据库）
void vip_dialog::on_delvipButton_clicked()
{
    int row = ui->vipTable->currentRow();
    if(row>=0) {
        QTableWidgetItem *vIdItem = ui->vipTable->item(row, 0);
        QString id = vIdItem->text();

        QSqlQuery query;
        QString sql = "delete from vip where v_id='" +id+ "';";
        if(query.exec(sql)){
            ui->vipTable->removeRow(row);
            QMessageBox::about(NULL, "提示", "删除成功");
        }
    }
}
